package Gui;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.StringTokenizer;

public class OpenNewCourse extends JPanel
{
	
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	Secretry main_frame;
	
	public OpenNewCourse(Secretry a) 
	{
		
		setLayout(null);
		main_frame=a;
		JLabel lblOpenNewCourse = new JLabel("Open New Course");
		lblOpenNewCourse.setFont(new Font("Tahoma", Font.PLAIN, 20));
		lblOpenNewCourse.setBounds(137, 11, 191, 25);
		add(lblOpenNewCourse);
		
		JLabel lblNewLabel = new JLabel("Couse ID");
		lblNewLabel.setBounds(33, 81, 105, 14);
		add(lblNewLabel);
		
		textField = new JTextField();
		textField.setBounds(163, 78, 86, 20);
		add(textField);
		textField.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("Course Name");
		lblNewLabel_1.setBounds(33, 127, 105, 14);
		add(lblNewLabel_1);
		
		textField_1 = new JTextField();
		textField_1.setBounds(163, 124, 86, 20);
		add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("Course Department");
		lblNewLabel_2.setBounds(33, 176, 126, 14);
		add(lblNewLabel_2);
		
		textField_2 = new JTextField();
		textField_2.setBounds(163, 173, 86, 20);
		add(textField_2);
		textField_2.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("Course Hours");
		lblNewLabel_3.setBounds(33, 219, 105, 14);
		add(lblNewLabel_3);
		
		textField_3 = new JTextField();
		textField_3.setBounds(163, 216, 86, 20);
		add(textField_3);
		textField_3.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel("Pre Course");
		lblNewLabel_4.setBounds(33, 260, 105, 14);
		add(lblNewLabel_4);
		
		textField_4 = new JTextField();
		textField_4.setBounds(163, 257, 86, 20);
		add(textField_4);
		textField_4.setColumns(10);
		
		JButton btnAddNewCourse = new JButton("Add new Course");
		
		
		btnAddNewCourse.addActionListener(new ActionListener() {					//action listener button add new course
			
			public void actionPerformed(ActionEvent e)
			{

				if(textField.getText().isEmpty()||textField_1.getText().isEmpty()||textField_2.getText().isEmpty()||textField_3.getText().isEmpty())
					showMsg("one of the filed are empty please fix end send again");
				else
					Open_Course(textField.getText(),textField_1.getText(),textField_2.getText(),textField_3.getText(),textField_4.getText());
			}
		});
		
		
		btnAddNewCourse.setBounds(279, 313, 133, 23);
		add(btnAddNewCourse);
		
		JButton btnBack = new JButton("Back");
		
		btnBack.addActionListener(new ActionListener() 		//action listener button back to the prev window
		{
			public void actionPerformed(ActionEvent e) 
			{
				main_frame.update_panel(5, 2);
			}
		});
		
		
		btnBack.setBounds(171, 313, 89, 23);
		add(btnBack);
		
		JLabel lblEnterTheNew = new JLabel("Enter the new course information:");
		lblEnterTheNew.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblEnterTheNew.setBounds(33, 56, 243, 14);
		add(lblEnterTheNew);
		
		JLabel lbluseFor = new JLabel("(use & key for more than 1)");
		lbluseFor.setBounds(268, 260, 172, 14);
		add(lbluseFor);
	}

	
		public void Open_Course(String course_id, String course_name, String course_dep, String course_hour, String pre_course)
		{
			if(pre_course.isEmpty())	//check if the new coursehave precourseis not we instalize to empty one
			pre_course=" ";
			
			String msg_check_id=new String("SELECT * FROM courses WHERE CourseID="+course_id);//check if the course is exsit in the same tranzaction to the server 
			String msg=new String("SELECT * FROM courses");//with the resulset we enter the course number
			main_frame.client.handleMessageFromClientUI("9;"+msg+";"+msg_check_id+";"+course_id+";"+course_name+";"+course_dep+";"+course_hour+";"+pre_course); //8 number of the window/ transaction 
			
			StringTokenizer st = new StringTokenizer (main_frame.client.Massage, ";", false);
			
			if(st.nextToken().equalsIgnoreCase("1")) 
					{
					showMsg1(st.nextToken());
					main_frame.update_panel(5, 2);
					textField.setText("");
					textField_1.setText("");
					textField_2.setText("");
					textField_3.setText("");
					textField_4.setText("");
					}
			else
				showMsg(st.nextToken());
		}
	
	
		public void showMsg(String msg)		//Func that sent error message 
		{
			JOptionPane.showMessageDialog(null,msg,"OpenNewCourse",JOptionPane.ERROR_MESSAGE);
		}
		
		public void showMsg1(String msg)		//Func that sent successfully message 
		{
			JOptionPane.showMessageDialog(null,msg,"OpenNewCourse",JOptionPane.INFORMATION_MESSAGE);
		}
}
